package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.WritRelevant;
import com.jiantong.lawEnforcing.service.WritRelevantService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 文书其它信息
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@OpenBean
public class WritRelevantApis {
    @RpcConsumer
    private WritRelevantService writRelevantService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.writRelevant.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public WritRelevant findById(Long id){
        Response<WritRelevant> response = writRelevantService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find writRelevant read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param writRelevant
     * @return
     */
    @OpenMethod(
            key = "create.writRelevant",
            paramNames = {"writRelevant"},
            httpMethods = RequestMethod.POST
    )
    public Long create(WritRelevant writRelevant){
        Response<Long> response = writRelevantService.create(writRelevant);
        if(!response.isSuccess()){
            log.error("failed to create writRelevant read by writRelevant = {}, cause : {}", writRelevant, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.writRelevant",
            paramNames = {"writRelevant"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(WritRelevant writRelevant){
        if(writRelevant.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = writRelevantService.update(writRelevant);
        if(!response.isSuccess()){
            log.error("failed to update writRelevant");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update writRelevant");
        }
        return response.getResult();
    }


}
